<template>
  <el-form
    ref="registerForm"
    :model="registerUser"
    :rules="registerRules"
    label-width="100px"
    class="registerForm sign-up-form"
  >
    <el-form-item label="用户名:" prop="name">
      <el-input
        v-model="registerUser.name"
        placeholder="请输入用户名"
      ></el-input>
    </el-form-item>
    <el-form-item label="邮箱:" prop="email">
      <el-input
        v-model="registerUser.email"
        placeholder="请输入邮箱"
      ></el-input>
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input
        v-model="registerUser.password"
        type="password"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item label="确认密码:" prop="password2">
      <el-input
        v-model="registerUser.password2"
        type="password"
        placeholder="请确认密码"
      ></el-input>
    </el-form-item>
    <el-form-item label="选择身份:">
      <el-select v-model="registerUser.role" placeholder="请选择身份">
        <el-option label="管理员" velue="admin">管理员</el-option>
        <el-option label="学生" velue="student">学生</el-option
        ><el-option label="游客" velue="visitor">游客</el-option>
        <el-option label="老师" velue="teacher">老师</el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button
        @click="handleRegister('registerForm')"
        type="primary"
        class="submit-btn"
        >注册</el-button
      >
    </el-form-item>
  </el-form>
</template>
<script lang="ts">
import { defineComponent, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
// import axios from "axios";
export default defineComponent({
  name: "RegisterForm",
  props: {
    registerUser: {
      type: Object,
      required: true,
    },
    registerRules: {
      type: Object,
      required: true,
    },
  },
  setup(props: any) {
    //@ts-ignore
    const { ctx } = getCurrentInstance();
    const router=useRouter();
    const handleRegister = (formName: string) => {
      ctx.$refs[formName].validate((valid: boolean) => {
        if (valid) {
          ctx.$axios.post(
            "/api/v1/auth/register",
            props.registerUser
          ).then((res:any)=>{
            //注册成功
            ctx.$message({
              message:"注册成功!",
              type:"success"
            })
            //路由跳转
            router.push("/")
          })
        } else {
          console.log("提交错误");
          return false;
        }
      });
    };
    return { handleRegister };
  },
});
</script>
<style scoped>

</style>